<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="format-detection" content="telephone=no">
    <title>JustChat</title>
    <link rel="stylesheet" href="../../../src/css/layui.css">
    <link rel="stylesheet" href="../../../src/css/layui.mobile.css?v=1.2.2">
    <link rel="stylesheet" href="../../justchat.css?v=1.2.2">
    <link rel="stylesheet" href="./otherpage.css?v=1.2.2">
    <link rel="stylesheet" href="./../../font/iconfont.css">
    <link rel="stylesheet" href="../../../src/css/modules/layim/mobile/layim.css?v=1.0.1">
    <script>
        function closeBrowser() {
            if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.ibc) {
                console.log("===");
                window.webkit.messageHandlers.ibc.postMessage({
                    'action': 'quit'
                }, "*");
            } else if (window.abc) {
                window.abc.quitBrowser();
            }
        }
    </script>
</head>

<body>
    <div id="unload" class="unload" style="display: none">
        <p><i class="layui-icon layui-icon-left" onclick="closeBrowser()"></i></p>
        <div style="margin-top: 160px;text-align: center;">
            <p>加载失败</p>
        </div>
        <div>
            <div style="width: 100px; border: 1px solid #ccc; text-align: center;border-radius: 10px;height: 40px;
                   line-height: 40px;margin: auto;background-color: #ff0;" onclick="location.reload()">点击重试</div>
        </div>
    </div>

    <div id="otherPage" style="position:relative;z-index:19891015">
        <div id="head" class="layim-title" style="background:#fff;z-index:19891015;text-align: center;">
            <i class="goback layui-icon layui-icon-left"></i>
            <p></p>
        </div>
        <div id="content" class="layim-content main-content" style="background:#f2f2f2;z-index:19891015;"></div>
    </div>


    <div id="menu">
        <p class="menu-delete-chat">删除对话</p>
    </div>

    <script src="../../../src/layui.js"></script>

    <script>
        // window.qinniuHost = 'http://im.bolansuzhou.com/'; //test
        window.qinniuHost = 'http://im.beierche.net/';
        // 代理默认头像
        window.agentDefultImg = ''
        // 玩家代理头像
        window.playerDefultImg = ''
        // 官方头像
        window.officialDefultImg = '';


        var baseExtend = "../../script/";
        layui.config({
            base: baseExtend,
            version: '1.2.2'
        }).use(['justchat', 'uiextend', 'justchatSocketMgr', 'justchatModel', 'justchatFunc', 'justchatCustom',
            'justchatApiMgr','complaint','userpage'
        ], function () {
            var layim = layui.justchat;
            var uiextend = layui.uiextend;
            var justchatFunc = layui.justchatFunc;
            var justchatSocketMgr = layui.justchatSocketMgr;
            var justchatModel = layui.justchatModel;
            var justchatCustom = layui.justchatCustom;
            var justchatApiMgr = layui.justchatApiMgr;

            var target_id = justchatSocketMgr.getParam('target_id')
            var imid = justchatSocketMgr.getParam('imid')
            var isAgency = window.location.search.includes('role=agent')
            // function successOnload(){
            //     if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.ibc) {
            //         console.log("===");
            //         window.webkit.messageHandlers.ibc.postMessage({
            //             'action': 'success_onload_page_fastpay'
            //         }, "*");
            //     } else if (window.abc) {
            //         window.abc.successOnloadPageFastpay();
            //     }
            // }
            // successOnload()
            if(isAgency){
                // 保证控制台缩放,告诉控制台去掉缩放和移动
                document.body.addEventListener('mouseup',function(){
                    window.parent.postMessage({type:'mouseup'},"*");
                },false)
            }



            function _updateChatStatus(index) {
                var presence_type = localStorage.getItem("presence_type") || "";
                var presence_type_chat = 0;
                if (presence_type) {
                    var list_presence_type = JSON.parse(presence_type);
                    presence_type_chat = list_presence_type[index];
                }
                uiextend.updateChatStatus(presence_type_chat);
            }

            layim.on('chat', function (index) {
                //官方运营不发起请求
                if(index == 'friend88888') return

                justchatFunc.justchatRequestTopic(justchatSocketMgr, index, justchatModel);
                console.warn("选项无法正常显示:isAgency=" + isAgency + "")
                if (!isAgency) {
                    justchatCustom.renderPayTypes(index.slice(6));
                }
            });

            layim.on('chatloaded', function (index) {
                uiextend.initPicEvent(layim);
            });


            layim.on('sendMessage', function (res) {
                justchatFunc.justchatSendMessage(justchatSocketMgr, res);
            });

            layim.on('sendPayTypeMessage', function (message) {
                var res = {
                    type: "pay:select_pay_type",
                    message: message
                };
                justchatCustom.sendMessage(justchatSocketMgr, res);
            });

            layim.on('sendPayAmountMessage', function (message) {
                var res = {
                    type: "pay:select_pay_amount",
                    message: message
                };
                justchatCustom.sendMessage(justchatSocketMgr, res);
            });

            justchatCustom.initCustom();
            // justchatSocketMgr.justChatSocketUrl = 'ws://3.1.171.240:3655';
            justchatSocketMgr.justChatSocketUrl = 'ws://chat.im.zfaya.com:9003'; //183.60.106.190

            justchatSocketMgr.init(eventCallback); //初始化socket

            if (target_id) {
                if(target_id == imid){
                    layer.msg('不能和自己对话')
                    return
                }
                chat(target_id)
            }

            function eventCallback(msgType, data) {
                console.log("justchat=》msg=" + msgType + " data=" + JSON.stringify(data));
                switch (msgType) {
                    case justchatModel.Msg_TYPE.ChatNT: {
                        eventChatNT(data);
                        break;
                    }
                    case justchatModel.Msg_TYPE.ChatReqAck: {
                        eventChatReqAck(data);
                        break;
                    }
                    // case justchatModel.Msg_TYPE.PresenceNT: {
                    //     eventPresenceNT(data);
                    //     break;
                    // }
                    case justchatModel.Msg_TYPE.AuthResp: {
                        eventAuthResp(data);
                        break;
                    }
                    case justchatModel.Msg_TYPE.ErrorST: {
                        eventErrorST(data);
                        break;
                    }
                    default:
                        break;
                }
            }

            var eventChatNT = function (data) {
                justchatFunc.justchatGetMessage(data, justchatCustom);
                justchatFunc.justchatClientAct(justchatSocketMgr);
            };

            var eventChatReqAck = function (data) {
                justchatFunc.justchatServerAck(data);
            };

            // var eventPresenceNT = function (data) {
            //     var id = data.ID;
            //     var type = data.Type; //0: 在线 1: 下线 2：休息离开 3：禁用
            //     //客户端维护一份，需要时从客户端读取
            //     var obj = {};
            //     if (localStorage.getItem("presence_type")) {
            //         obj = JSON.parse(localStorage.getItem("presence_type"));
            //     }
            //     obj["friend" + id] = type;
            //     localStorage.setItem("presence_type", JSON.stringify(obj));
            // };

            var eventAuthResp = function (data) {
                justchatFunc.justchatAuthResp(data);
            };

            var eventErrorST = function (data) {
                justchatFunc.justchateventErrorST(data);
            };

            function chat(target_id) {
                var onSucceedCallback = function (result) {
                    if (result.status == 0) {
                        var data = result.data
                        if (!(data.portraitUri)) {
                            data.portraitUri = ''
                        }
                        layim.chat({
                            name: data.nickname,
                            type: 'friend',
                            // avatar: data.portraitUri,
                            id: data.id
                        });
                        var friendsInfo = JSON.parse(localStorage.getItem("friendsInfo")) || {};
                        friendsInfo[target_id] = data
                        localStorage.setItem('friendsInfo', JSON.stringify(friendsInfo))
                    }
                };
                var onErrorCallback = function () {
                    // layer.msg('客服连接异常')
                };
                justchatApiMgr.getUserInfo(target_id, onSucceedCallback, onErrorCallback);
                if (!isAgency) {
                    justchatCustom.renderPayTypes(target_id);
                }
            };

        })
    </script>
    <script src="../../script/NewProtobuf.js"></script>
    <script src="../../script/plupload.full.min.js"></script>
    <script src="../../script/qiniu.min.js"></script>
</body>

</html>
